package com.bolt.support.ui.tree.vo;

import com.bolt.common.convert.Convert;
import com.bolt.common.utils.JacksonUtil;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.node.ObjectNode;

import java.io.IOException;

/**
 * Created by Administrator on 2021/2/2.
 */
public class UITreeNodeSerialize extends JsonSerializer<UITreeNode> {
    @Override
    public void serialize(UITreeNode treeNode, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        ObjectNode objectNode = JacksonUtil.getObjectMapper().createObjectNode();
        treeNode.getAttribute().forEach((k, v) -> {
            objectNode.put(Convert.toStr(k), Convert.toStr(v));
        });
        objectNode.put("id", treeNode.getId());
        objectNode.put("name", treeNode.getName());
        objectNode.put("title", treeNode.getTitle());
        objectNode.put("pid", treeNode.getPId());
        objectNode.put("url", treeNode.getUrl());
        if (!treeNode.isLeaf()) {
            objectNode.putPOJO("children", treeNode.getChildren());
        }
        objectNode.put("icon", treeNode.getIcon());
        objectNode.put("leaf", treeNode.isLeaf());
        objectNode.put("check", treeNode.isChecked());
        objectNode.put("hidden", treeNode.isHidden());
        objectNode.put("open", treeNode.isOpen());
        objectNode.put("isParent", treeNode.isParent());
        jsonGenerator.writeObject(objectNode);
    }
}

 